;
(function($) {
    function calendar(date, options) {
        //合并配置项
        this.options = $.extend({}, calendar.options, options);
        //记录原始日期
        this.origDate = calendar.changeDate(date);
        //记录选中日期对象
        this.checkedDate = new Date(this.origDate.getTime());
        //创建日历元素
        // this.calendarEle = $('<div class="think-calendar"></div>');
        //创建切换面板
        // this.switchPanelEle = $(calendar.createSwitchPanel.call(this)).appendTo(this.calendarEle);
        //创建日期面板
        // this.datePanelEle = $(calendar.createDatePanel.call(this)).appendTo(this.calendarEle);
        //创建月份面板
        // this.monthPanelEle = $(calendar.createMonthPanel.call(this)).appendTo(this.calendarEle);
        //创建年份面板
        // this.yearPanelEle = $('<div class="think-calendar-year"></div>').appendTo(this.calendarEle);
        //记录面板追加到指定的容器元素
        this.appendToEle = this.options.appendTo || $('body');
        //将日历元素添加到指定元素中
        // this.calendarEle.appendTo(this.appendToEle);
        //将日期面板初始切换到当前日期
        // this.targetDate(this.checkedDate);

        // this.yearPanelEle.html(calendar.getYearHtml(this.checkedDate.getFullYear()));
    }
    //************************Date******************************** 
    /**
     * 设置或获取最大日期值
     * @param date {Date} 最大取值范围
     * @return maxDate
     */
    calendar.prototype.maxDate = function(date) {

    }
    /**
     * 设置或获取最小日期值
     * @param date {Date} 最小取值范围
     * @return minDate
     */
    calendar.prototype.minDate = function(date) {

    }
    /**
     * 设置或获取无效的日期
     * @param date {Date} 无效的日期
     * @param satatus {Boolean}
     * @return minDate
     */
    calendar.prototype.invalidDate = function(date) {

    }
    /**
     * 设置或获取启用的日期
     * @param date {Date} 无效的日期
     * @param satatus {Boolean}
     * @return minDate
     */
    calendar.prototype.enableDate = function(date) {

    }
    //************************Panel******************************** 
    /**
     * 更改日历选择模式
     * @return calendar
     */
    calendar.prototype.changeMode = function() {

    }
    /**
     * 将面板切换到指定日期
     * @param date{Date} 有效的日期字符或日期对象
     * @return calendar
     */
    calendar.prototype.targetDate = function(date) {
        var _table = calendar.getDateHtml(date);
        this.datePanelEle.find('tbody').html(_table.panel);
        this.panelData = _table.data;
    }
    /**
     * 将面板切换到指定今天
     * @return calendar
     */
    calendar.prototype.today = function() {

    }
    /**
     * 复位
     * @summary 复位到初始状态
     * @return calendar
     */
    calendar.prototype.reset = function() {

    }
    calendar.options = {
        mode: 'date', //['year', 'month', 'date', 'datetime', 'time']
        maxDate: null,
        minDate: null,
        invalidDate: null, //["Y>2014","M=12","D=45"]
        enableDate: null,
        multiple: false,
        clear: true,
        format: 'YYYY-MM-DD hh:mm:ss',
        lang: {
            weeks: ['日', '一', '二', '三', '四', '五', '六'],
            months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
        },
        appendTo: null,
        praseData: null,
        onChange: null
    }
    /**
     * 创建日期选择面板
     * @param data {Date} 月份数据集
     * @param date {Date} 生成当前面板的日期对象
     * @return html
     */
    calendar.createDatePanel = function(data, date) {
        var _panel = '';
        _panel += '<div class="think-calendar-date">';
        _panel += '<table>';
        _panel += '<thead>';
        _panel += '<tr>';
        for (var i = 0; i < this.options.lang.weeks.length; i++) {
            _panel += '<th>' + this.options.lang.weeks[i] + '</th>';
        }
        _panel += '</tr>';
        _panel += '</thead>';
        _panel += '<tbody>';
        _panel += '</tbody>';
        _panel += '</table>';
        _panel += '</div>';
        return _panel;
    }
    /**
     * 获取日期选择项模版
     * @param date {Date} 有效的日期字符或日期对象
     * @return html
     */
    calendar.getDateHtml = function(date) {
        var _date = calendar.changeDate(date);
        var _data = calendar.dateTable(_date);
        var _today = calendar.getTodayTime();
        var _panel = '<tr>',
            _length = _data.length,
            _month = _date.getMonth() + 1,
            _week = [0, 6];
        for (var i = 1; i <= _length; i++) {
            var _n = i - 1;
            _panel += '<td class="think-calendar-day';
            _panel += $.inArray(_data[_n].week, _week) >= 0 ? ' think-calendar-weekend' : '';
            _panel += _data[_n].month != _month ? ' think-calendar-othermonth' : '';
            _panel += _data[_n].time == _today ? ' think-calendar-today' : '';
            _panel += '">' + _data[_n].day + '</td>';
            _panel += i % 7 || i > _length - 7 ? '' : '</tr><tr>';
        }
        _panel += '</tr>';
        return {
            data: _data,
            panel: _panel
        };
    }
    /**
     * 获取今天的时间戳(不带时间)
     * @return time
     */
    calendar.getTodayTime = function() {
        var _date = new Date(),
            _year = _date.getFullYear(),
            _month = _date.getMonth(),
            _date = _date.getDate();
        return new Date(_year,_month,_date).getTime();
    }
    //创建选择切换面板
    calendar.createSwitchPanel = function(){
        var _panel = '';
        _panel += '<div class="think-calendar-switch">';
        _panel += '<b class="think-calendar-switch-prev">&lt;</b>';
        _panel += '<span class="think-calendar-switch-current">2012-12-12</span>';
        _panel += '<b class="think-calendar-switch-next">&gt;</b>';
        _panel += '</div>';
        return _panel;
    }
    /**
     * 创建月份选择面板
     * @return html
     */
    calendar.createMonthPanel = function() {
        var
        _length = this.options.lang.months.length,
            _panel = '';
        _panel += '<div class="think-calendar-month">';
        _panel += '<table>';
        _panel += '<tr>';
        for (var i = 1; i <= _length; i++) {
            _panel += '<th>' + this.options.lang.months[i - 1] + '</th>';
            _panel += i % 4 || i > _length - 4 ? '' : '</tr><tr>';
        }
        _panel += '</tr>';
        _panel += '</table>';
        _panel += '</div>';
        return _panel;
    }
    /**
     * 获取年份选择项模版
     * @return html
     */
    calendar.getYearHtml = function(year) {

    }
    /**
     * 获取指定日期的所在月份的日程表
     * @param date {Date} 日期对象
     * @return Array
     */
    calendar.dateTable = function(date) {
        var
        _step = 0,
            _data = [],
            _date = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        _date.setDate(1);
        _date.setDate(1 - _date.getDay());
        for (var i = 0; i < 42; i++) {
            _date.setDate(_date.getDate() + _step);
            _step = 1;
            _data.push({
                year: _date.getFullYear(),
                month: _date.getMonth() + 1,
                day: _date.getDate(),
                week: _date.getDay(),
                time: _date.getTime()
            });
        }
        return _data;
    }
    calendar.changeDate = function(date) {
        if ($.type(date) != 'date') {
            date = date.toString();
            date = date.match(/([0-9]+)/g);
            return new Date(
                date[0] || 0,
                date[1] ? date[1] - 1 : 0,
                date[2] || 0,
                date[3] || 0,
                date[4] || 0,
                date[5] || 0
            );
        }
        return date;
    }
    calendar.formatDate = function(date, format) {
        var _date = date;
        if (typeof _date == 'string') {
            var _arr = _date.split(/\D/);
            var _date = new Date(_arr[0], _arr[1] - 1, _arr[2], _arr[3], _arr[4], _arr[5]);
        }
        var o = {
            "M+": _date.getMonth() + 1,
            "D+": _date.getDate(),
            "h+": _date.getHours(),
            "m+": _date.getMinutes(),
            "s+": _date.getSeconds(),
            "q+": Math.floor((_date.getMonth() + 3) / 3),
            "S": _date.getMilliseconds()
        };
        if (/(Y+)/.test(format)) {
            format = format.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
    $.calendar = function(date, options) {
        return new calendar(date, options);
    }
    $.calendar.dateTable = calendar.dateTable;
    $.calendar.changeDate = calendar.changeDate;
    $.calendar.formatDate = calendar.formatDate;
    $.fn.calendar = function() {

    }
})(jQuery);
